//
//  enum_test.swift
//  cnswift-demo
//
//  Created by YangWei on 2020/12/18.
//  Copyright © 2020 YangWei. All rights reserved.
//

import UIKit

enum Rank: Int {
    case ace = 1
    case two, three, four, five, six, seven, eight, nine, ten
    case jack, queen, king
    func simpleDescription() -> String {
        switch self {
        case .ace:
            return ".ace"
        case .jack:
            return "jack"
        case .queen:
            return "queen"
        case .king:
            return "king"
        default:
            return String(self.rawValue)
        }
    }
    /**
    实验
    写一个函数通过对比它们的原始值来对比两个 Rank 值
    */
    func compare(rank: Rank) -> String {
        if self.rawValue > rank.rawValue {
            return "\(self.simpleDescription()) > \(rank.simpleDescription())"
        } else if self.rawValue < rank.rawValue {
            return "\(self.simpleDescription()) < \(rank.simpleDescription())"
        } else {
            return "\(self.simpleDescription()) = \(rank.simpleDescription())"
        }
    }
}

// 不需要原始值的枚举
enum Suit {
    case spades, hearts, diamonds, clubs
    func simpleDescription() -> String {
        switch self {
        case .spades:
            return "spades"
        case .hearts:
            return "hearts"
        case .diamonds:
            return "diamonds"
        case .clubs:
            return "clubs"
        }
    }
    /**
    实验
    添加一个 color()方法到 Suit，为黑桃和梅花返回“black”，为红桃和方片返回“red”。
    */
    func color() -> String {
        switch self {
        case .spades, .clubs:
            return "black"
        case .hearts, .diamonds:
            return "red"
        }
    }
}

// 枚举的初始值在实例初始化时确定
enum ServerResponse {
    case result(String, String)
    case failure(String)
    /**
     实验
     添加第三个 case 到 ServerResponse 和 switch。
     */
    case timeout(String)
}

class enum_test: NSObject {
    func showRawValue() {
        let ace = Rank.ace
        let aceRawValue = ace.rawValue
        print("aceRawValue = \(aceRawValue)")
    }
    
    func compareTest() {
        let king = Rank.king
        let jack = Rank.jack
        let result = king.compare(rank: jack);
        print(result)
    }
    
    func initCreateTest() {
        if let convertedRank = Rank(rawValue: 3) {
            let threeDescription = convertedRank.simpleDescription()
            print(threeDescription)
        }
    }
    
    func noRawValueEnumTest() {
        let hearts = Suit.hearts
        let heartsDescription = hearts.simpleDescription()
        print(heartsDescription)

        let spades = Suit.spades
        let diamonds = Suit.diamonds
        let clubs = Suit.clubs
        print("""
            Suit color:
            hearts color is \(hearts.color())
            spades color is \(spades.color())
            diamonds color is \(diamonds.color())
            clubs color is \(clubs.color())
            """)
    }
    
    func rawValueAfterInitTest() {
        let success = ServerResponse.result("6:00 am", "6:00 am")
        let failure = ServerResponse.failure("Out of cheese.")
        let timout = ServerResponse.timeout("I'm waiting result....")
        
        let resultType = 2 // 0 1 2
        
        let result: ServerResponse
        
        if resultType == 0 {
            result = success
        } else if resultType == 1 {
            result = failure
        } else {
            result = timout
        }
        
        switch result {
        case let .result(sunrise, sunset):
            print("Sunrise is at \(sunrise) and sunset is at \(sunset).")
        case let .failure(message):
            print("Failure...  \(message)")
        case let .timeout(message):
            print("Time out... \(message)")
        }
    }
}


